package com.health.ts.constant;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;

import lombok.Data;

/**
 * 统一返回消息体
 */
@Data
public class ApiResult implements Serializable {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2347975073088733151L;

	/**
	 * 状态码
	 */
	private String code;
	
	/**
	 * 提示消息
	 */
	private String message;
	
	/**
	 * 返回数据
	 */
	private Object payload;
	
	/**
	 * 构造方法1
	 * @param code
	 * @param message
	 */
	public ApiResult(String code, String message) {
		super();
		this.code = code;
		this.message = message;
	}
	
	/**
	 * 构造方法2
	 * @param code
	 * @param message
	 * @param payload
	 */
	public ApiResult(String code, String message, Object payload) {
		this(code, message);
		this.payload = payload;
	}
	
	/**
	 * 返回成功（没有返回数据）
	 * @return
	 */
    public static ApiResult success() {
		return success(null);
    }
	
	/**
	 * 返回成功（有返回数据）
	 * @param payload
	 * @return
	 */
    public static ApiResult success(Object payload) {
		return new ApiResult(ResultCodeEnum.SUCCESS.value(), ResultCodeEnum.SUCCESS.getDes(), payload);
    }
    
    /**
     * 返回业务失败
	 * @param code
     * @param msg
     * @return
     */
    public static ApiResult failed(String code, String msg) {
		return new ApiResult(code, msg);
    }
    
    /**
     * 返回业务失败
     * @param msg
     * @return
     */
    public static ApiResult failed(String msg) {
		return failed(ResultCodeEnum.BUSINESS_FAILED.value(), msg);
    }
    
    /**
     * 返回系统错误
     * @param msg
     * @return
     */
    public static ApiResult error(String msg) {
		return new ApiResult(ResultCodeEnum.SYSTEM_ERROR.value(), msg);
    }
    
    public String toJson() {
    	return JSON.toJSONString(this);
    }
}
